package com.controller.qd;


import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.controller.base.BaseController;
import com.entity.About;
import com.entity.AppendixRelationship;
import com.entity.ColumnClass;
import com.entity.Information;
import com.entity.Login;
import com.entity.NewsColumn;
import com.entity.Product;
import com.entity.User;
import com.entity.UserIndustry;
import com.service.AboutService;
import com.service.AppendixRelationshipService;
import com.service.ColumnClassService;
import com.service.ProductService;
import com.service.UserIndustryService;
import com.service.UserService;
import com.service.informationService;
import com.service.newsColumnService;

@Controller
@RequestMapping("/qd/")
public class qdFwController extends BaseController<Object>{
	@Autowired
	private newsColumnService newservice;
	@Autowired
	private ColumnClassService ccservice;
	@Autowired
	private informationService infoservice;
	@Autowired
	private ProductService proservice;
	@Autowired
	private AboutService aboutservice;
	@Autowired
	private AppendixRelationshipService apprpservice;
	@Autowired
	private UserService userservice;
	@Autowired
	private UserIndustryService uiservice;
	@RequestMapping("/bangzhuzhongxin")
	public ModelAndView bangzhuzhongxin(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/bangzhuzhongxin");
		return mv;
	}
	@RequestMapping("/bendiZhengce")
	public ModelAndView bendiZhengce(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/bendiZhengce");
		return mv;
	}
	@RequestMapping("/cpsc")
	public ModelAndView cpsc(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/cpsc");
		return mv;
	}
	@RequestMapping("/duijiechenggong")
	public ModelAndView duijiechenggong(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/duijiechenggong");
		return mv;
	}
	@RequestMapping("/duijiejieguo")
	public ModelAndView duijiejieguo(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/duijiejieguo");
		return mv;
	}
	@RequestMapping("/fuwupingji")
	public ModelAndView fuwupingji(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/fuwupingji");
		return mv;
	}
	@RequestMapping("/login")
	public ModelAndView login(String xuanxiang){
		ModelAndView mv =  new ModelAndView();
		if(xuanxiang==null){
			xuanxiang="0";
		}
		mv.addObject("xuanxiang",xuanxiang);
		mv.setViewName("qdPages/login");
		return mv;
	}
	@RequestMapping("/renwuxuanze")
	public ModelAndView renwuxuanze(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/renwuxuanze");
		return mv;
	}
	@RequestMapping("/xinyongpingji")
	public ModelAndView xinyongpingji(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("qdPages/xinyongpingji");
		return mv;
	}
	@RequestMapping("/zhucezhuangtaiwode")
	public ModelAndView zhucezhuangtaiwode(User user,String userCLassNo){
		ModelAndView mv =  new ModelAndView();
		HttpSession session = getRequest().getSession();
		Login login = (Login)session.getAttribute("login");
		user.setLoginNo(login.getLoginAccount());
		List<User> userUis = userservice.find(user);
		mv.addObject("userUis", userUis.get(0));
		mv.addObject("userCLassNo",userCLassNo);
		mv.setViewName("qdPages/zhucezhuangtaiwode");
		return mv;
	}
	@RequestMapping("/zhucezhuangtai")
	public ModelAndView zhucezhuangtai(User user,String userCLassNo){
		ModelAndView mv =  new ModelAndView();
		user.setUserCLassNo(userCLassNo);
		List<User> userUis = userservice.find(user);
		mv.addObject("userUis", userUis);
		mv.setViewName("qdPages/zhucezhuangtai");
		return mv;
	}
	@RequestMapping("/suqiujianyi")
	public ModelAndView suqiujianyi(User user,String userId){
		ModelAndView mv =  new ModelAndView();
		user.setId(userId);
		List<User> userUis = userservice.find(user);
		mv.addObject("user", userUis.get(0));
		mv.setViewName("qdPages/suqiujianyi");
		return mv;
	}
	@RequestMapping("/suqiujianyisuoyou")
	public ModelAndView suqiujianyisuoyou(User user,String userCLassNo){
		ModelAndView mv =  new ModelAndView();
		user.setUserCLassNo(userCLassNo);
		List<User> userUis = userservice.find(user);
		mv.addObject("userUis", userUis);
		mv.addObject("userCLassNo",userCLassNo);
		mv.setViewName("qdPages/suqiujianyisuoyou");
		return mv;
	}
	@RequestMapping("/lanmu")
	public ModelAndView xinwenliebiao(NewsColumn n,String cid,String cname){
		ModelAndView mv =  new ModelAndView();
		n.setColumnClassId(cid);
		List<NewsColumn> nUis = newservice.findColumnByClass(n);
		if(nUis.size()!=0){
			mv.addObject("nuis", nUis);
		}
		mv.addObject("cname", cname);
		mv.setViewName("qdPages/xinwenliebiao");
		return mv;
	}
	/**
	 * 从表中查出栏目信息和最新的新闻资讯
	 * @param cc
	 * @param info
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(ColumnClass cc,Information info,AppendixRelationship apprp){
		ModelAndView mv =  new ModelAndView();
		apprp.setRelationTName("封面");
		List<ColumnClass> cUis = ccservice.find(cc);
		List<Information> iuis = infoservice.findByTime(info);
		List<AppendixRelationship> auis = apprpservice.find(apprp);
		if(cUis.size()!=0){
			mv.addObject("cuis", cUis);
		}
		String a = iuis.get(0).getNewsContent();
		String b = iuis.get(1).getNewsContent();
		if(a.length()>20){
			a = iuis.get(0).getNewsContent().substring(0, 20);
		}
		if(b.length()>20){
			b = iuis.get(1).getNewsContent().substring(0, 20);
		}
		mv.addObject("iuisa",a);
		mv.addObject("iuisb",b);
		mv.addObject("iuisIda",iuis.get(0).getId());
		mv.addObject("iuisIdb",iuis.get(1).getId());
		System.out.println(auis.get(0).toString()+"=======================");
		mv.addObject("auis",auis);
		mv.setViewName("qdPages/index");
		return mv;
	}
	@RequestMapping("/tuijianchanpin")
	public ModelAndView tuijianchanpin(Product pro,UserIndustry ui,String industryName){
		ModelAndView mv =  new ModelAndView();
		//设置产品分类
		pro.setClassification(industryName);
		List<Product> pList = proservice.find(pro);
		List<UserIndustry> uiList = uiservice.find(ui);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < pList.size(); i++) {
			//转换时间格式
			pList.get(i).setTimePString(sf.format(pList.get(i).getTimeP()));
			pList.get(i).setReleaseTimeString(sf.format(pList.get(i).getReleaseTime()));
		}
		mv.addObject("pro", pList);
		mv.addObject("ui", uiList);
		mv.addObject("industryName",industryName);
		mv.setViewName("qdPages/tuijianchanpin");
		return mv;
	}
	@RequestMapping("/rongzichanpin")
	public ModelAndView rongzichanpin(Product pro,UserIndustry ui,String industryName){
		ModelAndView mv =  new ModelAndView();
		//设置产品分类
		pro.setClassification(industryName);
		List<Product> pList = proservice.find(pro);
		List<UserIndustry> uiList = uiservice.find(ui);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < pList.size(); i++) {
			//转换时间格式
			pList.get(i).setTimePString(sf.format(pList.get(i).getTimeP()));
			pList.get(i).setReleaseTimeString(sf.format(pList.get(i).getReleaseTime()));
		}
		mv.addObject("pro", pList);
		mv.addObject("ui", uiList);
		mv.addObject("industryName",industryName);
		mv.setViewName("qdPages/rongzichanpin");
		return mv;
	}
	@RequestMapping("/rongzizhuangtai")
	public ModelAndView rongzizhuangtai(Product pro,UserIndustry ui,String industryName){
		ModelAndView mv =  new ModelAndView();
		//设置产品分类
		pro.setClassification(industryName);
		List<Product> pList = proservice.find(pro);
		List<UserIndustry> uiList = uiservice.find(ui);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < pList.size(); i++) {
			//转换时间格式
			pList.get(i).setTimePString(sf.format(pList.get(i).getTimeP()));
			pList.get(i).setReleaseTimeString(sf.format(pList.get(i).getReleaseTime()));
		}
		mv.addObject("pro", pList);
		mv.addObject("ui", uiList);
		mv.addObject("industryName",industryName);
		mv.setViewName("qdPages/rongzizhuangtai");
		return mv;
	}
	@RequestMapping("/about")
	public ModelAndView about(About about,String aid){
		ModelAndView mv =  new ModelAndView();
		about.setId(aid);
		List<About> aUis = aboutservice.find(about);
		mv.addObject("auis",aUis.get(0));
		mv.setViewName("qdPages/about");
		return mv;
	}
	@RequestMapping("/newInfo")
	public ModelAndView newInfo(Information info,String nid){
		ModelAndView mv =  new ModelAndView();
		info.setId(nid);
		List<Information> infoUis = infoservice.find(info);
		NewsColumn n = new NewsColumn();
		n.setId(infoUis.get(0).getColumnNumber());
		List<NewsColumn> nuis = newservice.find(n);
		infoUis.get(0).setColumnName(nuis.get(0).getColumnName());
		mv.addObject("infoUis",infoUis.get(0));
		mv.setViewName("qdPages/newInfo");
		return mv;
	}
	@RequestMapping("/xiangguanchanpin")
	public ModelAndView xiangguanchanpin(Product p,String pid){
		ModelAndView mv =  new ModelAndView();
		System.out.println(pid);
		p.setId(pid);
		List<Product> puis = proservice.find(p);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < puis.size(); i++) {
			//转换时间格式
			puis.get(i).setTimePString(sf.format(puis.get(i).getTimeP()));
			puis.get(i).setReleaseTimeString(sf.format(puis.get(i).getReleaseTime()));
		}
		mv.addObject("puis",puis.get(0));
		mv.setViewName("qdPages/xiangguanchanpin");
		return mv;
	}
}
